package org.JWCB.system.controller.sysuser;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.JWCB.common.core.constants.HttpConstants;
import org.JWCB.common.core.controller.BaseController;
import org.JWCB.common.core.domain.vo.LoginUserVo;
import org.JWCB.common.core.domain.vo.R;
import org.JWCB.system.domain.sysuser.dto.SysUserLoginDTO;
import org.JWCB.system.domain.sysuser.dto.SysUserSaveDTO;
import org.JWCB.system.service.file.IFileService;
import org.JWCB.system.service.sysuser.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("sysUser")
@Tag(name = "管理员用户API")
public class SysUserController extends BaseController {
    @Autowired
    private ISysUserService sysUserService;


    @Autowired
    private IFileService fileService;

    @Operation(summary = "登录接口",description = "根据账号密码进行登录")
    @ApiResponse(responseCode = "1000",description = "操作成功")
    @ApiResponse(responseCode = "2000",description = "服务器繁忙请稍后重试")
    @ApiResponse(responseCode = "3103",description = "用户名或密码错误")
    @ApiResponse(responseCode = "3102",description = "用户不存在")
    @PostMapping("/login")
    public R<String> login(@RequestBody @Validated SysUserLoginDTO loginDTO) {
        log.info("用户{}登录",loginDTO == null ? null : loginDTO.getUserAccount());
        assert loginDTO != null;
        return sysUserService.login(loginDTO.getUserAccount(),loginDTO.getPassword());
    }


    @PostMapping({"/add"})
    @Operation(summary = "新增管理员接口",description = "根据账号密码新增管理员")
    @ApiResponse(responseCode = "1000",description = "操作成功")
    @ApiResponse(responseCode = "2000",description = "服务器繁忙请稍后重试")
    @ApiResponse(responseCode = "3101",description = "用户已存在")
    public R<Void> add(@RequestBody @Validated SysUserSaveDTO sysUserSaveDTO) {
        return toR(sysUserService.add(sysUserSaveDTO));
    }


    @GetMapping("/info")
    public R<LoginUserVo> info(@RequestHeader(HttpConstants.AUTHENTICATION) String token) {
        return sysUserService.info(token);
    }



    @DeleteMapping("/logout")
    public R<Void> logout(@RequestHeader(HttpConstants.AUTHENTICATION) String token) {
        return toR(sysUserService.logout(token));
    }




}
